package com.cn.ant.modules.wechat.web;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.utils.AjaxJson;
import com.cn.ant.common.utils.DateUtils;
import com.cn.ant.common.utils.Identities;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.sys.entity.User;
import com.cn.ant.modules.sys.utils.UserUtils;
import com.cn.ant.modules.wechat.entity.WeiXinNewListItem;
import com.cn.ant.modules.wechat.entity.WeiXinNewListType;
import com.cn.ant.modules.wechat.service.WeiXinNewListItemService;
import com.cn.ant.modules.wechat.service.WeiXinNewListTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 消息列表条目Controller
 * @author AntDream
 * @version 2015-08-01
 */
@Controller
@RequestMapping(value = "${adminPath}/wechat/weiXinNewListItem")
public class WeiXinNewListItemController extends BaseController {

	@Autowired
	private WeiXinNewListItemService weiXinNewListItemService;
    @Autowired
    private WeiXinNewListTypeService newListTypeService;
	
	@ModelAttribute
	public WeiXinNewListItem get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return weiXinNewListItemService.get(id);
		}else{
			return new WeiXinNewListItem();
		}
	}
	
	@RequiresPermissions("wechat:weiXinNewListItem:view")
	@RequestMapping(value = {"list", ""})
	public String list(WeiXinNewListItem weiXinNewListItem, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			weiXinNewListItem.setUserId(user.getId());
		}
        Map<String,Object> param = new HashMap<String,Object>();
        Page<WeiXinNewListItem> page = weiXinNewListItemService.find(new Page<WeiXinNewListItem>(request, response), param);
        model.addAttribute("page", page);
		return "modules/wechat/weiXinNewListItemList";
	}

	@RequiresPermissions("wechat:weiXinNewListItem:view")
	@RequestMapping(value = "form")
	public String form(WeiXinNewListItem weiXinNewListItem, Model model) {
		model.addAttribute("weiXinNewListItem", weiXinNewListItem);
        List<WeiXinNewListType> newListTypeList = newListTypeService.queryByLoginUser();
        model.addAttribute("newListTypeList", newListTypeList);
        return "modules/wechat/weiXinNewListItemForm";
	}

	@RequiresPermissions("wechat:weiXinNewListItem:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxJson save(WeiXinNewListItem weiXinNewListItem, Model model) {
        AjaxJson jo = new AjaxJson();
        User user = UserUtils.getUser();
        try {
			if (!beanValidator(model, weiXinNewListItem)) {
                jo.setSuccess(false);
                jo.setMsg("数据校验失败,请重试!");
			} else {
				if (StringUtils.isBlank(weiXinNewListItem.getId())) {
					weiXinNewListItem.setId(Identities.generateUUID());
                    weiXinNewListItem.setOfficeId(user.getOffice().getId());
                    weiXinNewListItem.setUserId(user.getId());
                    weiXinNewListItem.setUpdateTime(DateUtils.getCurrDate());
                    weiXinNewListItemService.save(weiXinNewListItem);
				} else {
                    weiXinNewListItem.setUpdateTime(DateUtils.getCurrDate());
					weiXinNewListItemService.update(weiXinNewListItem);
				}
			}
			msg = "保存消息列表条目'" + weiXinNewListItem.getTitle() + "'成功";
            jo.setMsg(msg);
		} catch (Exception e) {
			e.printStackTrace();
            jo.setSuccess(false);
            jo.setMsg("操作失败,请联系管理员");
		}
		return jo;
	}
	
	@RequiresPermissions("wechat:weiXinNewListItem:edit")
	@RequestMapping(value = "delete")
    @ResponseBody
	public AjaxJson delete(String id, RedirectAttributes redirectAttributes) {
        AjaxJson jo = new AjaxJson();
        if (StringUtils.isNotBlank(id)) {
            weiXinNewListItemService.delete(id);
            jo.setMsg("删除成功,页面在2s后跳转..");
        } else {
            jo.setSuccess(false);
            jo.setMsg("参数错误,删除失败");
        }
        return jo;
	}

}
